/*
 *
 * Copyright (c) 2021
 * User:Dybala
 * File:VerificationUtils.java
 * Date:2021/11/13 22:18:13
 */

package com.utils;

import com.service.redisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Random;

/**
 * 用于校验验证码是否正确
 */
@Component
public class VerificationUtils {
   private static redisService redisService;

   @Autowired
    public VerificationUtils(redisService service) {
       VerificationUtils.redisService=service;
    }

    /**
     * 用于校验用户输入的验证码和redis的验证码是否一致
     * @param email 用户输入的邮箱
     * @param code 用户输入的验证码
     * @return 返回是否输入正确
     */
    public static boolean verify(String email,String code){
        if(code==null)return false;
        String str =redisService.get(email);
        return code.equals(str);
    }
    public static String generateCode(){
        String codeNum = "";
        int [] code = new int[3];
        Random random = new Random();
        for (int i = 0; i < 4; i++) {
            int num = random.nextInt(10) + 48;
            int uppercase = random.nextInt(26) + 65;
            int lowercase = random.nextInt(26) + 97;
            code[0] = num;
            code[1] = uppercase;
            code[2] = lowercase;
            codeNum+=(char)code[random.nextInt(3)];
        }
        System.out.println(codeNum);

        return codeNum;

    }
}
